/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poker.geneticAlgorithm;

import java.util.Iterator;
import java.util.List;

/**
 * Selector which selects the individual with the highest value of
 * added values. Note that
 * this is an implicit fitness function as well. Used for testing a genetic
 * algorithm.
 * 
 * 
 * @author Benjamin L. Brodie <blbrodie@gmail.com>
 */
public class HighestValueSelector implements Selection<Individual<Double>>{

    public Individual<Double> selectFrom(List<Individual<Double>> individuals) {
        Individual<Double> winner = individuals.get(0);
        Double greatestTotal = addAllValues(individuals.get(0).getList());
        
        Individual<Double> current;
        double totalValue;
        for (Iterator<Individual<Double>> iter = individuals.iterator(); iter.hasNext();){
            current = iter.next();
            totalValue = addAllValues(current.getList());
            
            if (totalValue > greatestTotal){
                greatestTotal = totalValue;
                winner = current;
            }
        }
        
        return winner;
    }
    
    private double addAllValues(List<Double> values){
        double total = 0;
        
        for (Double value : values){
            total += value;
        }
        
        return total;
    }
    
}
